package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.StringField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataString extends StringField implements IPrimative{
	public static String DEFAULT_ID="_String";

	private TagModifier _modifier = TagModifier.STRING_TYPE_STRING;

public DataString() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public DataString(String value) {

	super(value);

	try {
		setId(DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public DataString(TagModifier type) {

	super();

	_modifier = type;

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public DataString(TagModifier type,String value) {

	super(value);

	_modifier = type;

	try {
		setId(_modifier.DEFAULT_ID, this);
	} catch (LockException le) {
	} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml) {

 if(_modifier.DEFAULT_ID.equals("_File"))
 {
	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}


	String value;

	value = xml.getAttributeValueByName(Token.URL);
	if(value!=null)
	{
		try{
			if(xml.getAttributeValueByName(Token.SOURCE)!=null)
			{
				String url = xml.getAttributeValueByName(Token.SOURCE);
				url = url.substring(0,url.lastIndexOf("/")+1)+value;
				setValue(_modifier.getAttributeName(Token.URL),url,this);
			}
			else
			{
				setValue(_modifier.getAttributeName(Token.URL),value,this);
			}
		}
		catch(LockException le){}//dummy catch
	}
 }
 else
 {
	String attribName;
	String attribValue;

	for(int loop=0;loop<xml.getAttributeCount();loop++)
	{
		attribName = xml.getAttributeNameByIndex(loop);
		attribValue = xml.getAttributeValueByIndex(loop);

		if(attribName.equals(Token.ID))
		{
			if(attribValue!=null)
			{
				try{
					setId(attribValue,this);
				}
				catch(LockException le){} //Dummy catch
			}
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.VALUE)))
		{
			try{
				setValue(Token.VALUE,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else if( !NameHelper.isSystemName(attribName) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_ATTRIB_CODE ,new Object[]{attribName,Token.STRING,""+xml.getStartLineNo()} );
		}
	}
 }
}
public void copyFromgrfxML(XmlElement xml) {

	String attribName;
	String attribValue;

	for(int loop=0;loop<xml.getAttributeCount();loop++)
	{
		attribName = xml.getAttributeNameByIndex(loop);
		attribValue = xml.getAttributeValueByIndex(loop);

		if(attribName.equals(Token.ID))
		{
			// do nothing but not an error
		}
		else if(attribName.equals(_modifier.getAttributeAlias(Token.VALUE)))
		{
			try{
				setValue(Token.VALUE,attribValue,this);
			}
			catch(LockException le){}//dummy catch
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_ATTRIB_CODE ,new Object[]{attribName,Token.STATE,""+xml.getStartLineNo()} );
		}
	}
}
public DataString getClone() {

	DataString newString = new DataString(_modifier);
	try{
		newString.setValue(getValue(),this);
	}catch(LockException le){}// dummy catch

	return newString;
}
public DataString getClone(String path) {

	DataString newString = getClone();

	GrfxMLStoreProcessor.setCloneName( path,this,newString );

	return newString;
}
public TagModifier getModifier() {
	return _modifier;
}
public void register(DataLinker store) {
	super.register(store);
}
public void replace(Field oldObj, Field newObj) {
}
public void replace(Field oldObj, Field newObj,boolean recursive) {
}
public void resetChanged() {
	super.resetChanged();
}
public String togrfxML(String name) {
	return IPrimativeHelper.togrfxML(this,name);
}
public String togrfxML(String name,TagModifier modifier) {
	return IPrimativeHelper.togrfxML(this,name,modifier);
}
public String togrfxML(IPrimative defaultField,String name) {
	return IPrimativeHelper.togrfxML(defaultField,this,name,null);
}
public String togrfxML(IPrimative defaultField,String name,TagModifier modifier) {
	return IPrimativeHelper.togrfxML(defaultField,this,name,modifier);
}

public String toString() {
	return IPrimativeHelper.toString(this);
}
}
